/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.graficaluna.dao.otros;

import dto.MaquinaDTO;
import dto.OrdenTrabajoDTO;
import java.sql.Connection;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author ralf
 */
public class DistribucionOrdenMaquinaDAO {
    
    private SqlSessionFactory sqlSessionFactory;

    public DistribucionOrdenMaquinaDAO() {
        sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    }
    
    public Connection obtenerConnexion(){
        SqlSession session = sqlSessionFactory.openSession();
        return session.getConnection();
    }
    
    public void insertarOrdenMaquina(OrdenTrabajoDTO p){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.insert("distribucionOrdenMaquinaSQL.insertarOrdenMaquina", p);        
            session.commit();
        } finally {
            session.close();
        }
    }
    
    public void actualizarOrdenMaquina(OrdenTrabajoDTO p) {
        SqlSession session = sqlSessionFactory.openSession();      
        try {
            session.update("distribucionOrdenMaquinaSQL.actualizarOrdenMaquina", p);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    public void eliminarOrdenMaquina(OrdenTrabajoDTO p) {
        SqlSession session = sqlSessionFactory.openSession();      
        try {
            session.delete("distribucionOrdenMaquinaSQL.eliminarOrdenMaquina", p);
            session.commit();
        } finally {
            session.close();
        }
    }
        
    public List<MaquinaDTO> listarOrdenMaquina(Long codigo) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<MaquinaDTO> list = session.selectList("distribucionOrdenMaquinaSQL.listarOrdenMaquina",codigo);
            return list;
        }catch(Exception e){  
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }
    }
    
    //INVOCA A UNA FUNCION DEL POSTGRES
    public void distribuirOrdenMaquina() {
        SqlSession session = sqlSessionFactory.openSession();      
        try {                  
            session.selectList("distribucionOrdenMaquinaSQL.distribuirOrdenMaquina");
            session.commit();            
        } finally {
            session.close();
        }
    }
    
}
